package com.icoolkj.learn.juc.future;

import com.icoolkj.learn.juc.base.utils.SleepTools;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/03/05 16:57
 */
@Slf4j
public class FutureTaskDemo3 {

    public static void main(String[] args) {
        // 创建任务T2的 FutureTask
        FutureTask<String> ft2 =new FutureTask<>(new T2Task());
        // 创建任务T1的 FutureTask
        FutureTask<String> ft1 =new FutureTask<>(new T1Task(ft2));

        Thread thread1 = new Thread(ft1);
        Thread thread2 = new Thread(ft2);
        thread1.start();
        thread2.start();
        try {
            log.info(ft1.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

}

// 洗水壶，烧开水，泡茶
class T1Task implements Callable<String> {
    private static final Logger log = LoggerFactory.getLogger(T1Task.class);
    FutureTask<String> ft2;

    public T1Task(FutureTask<String> ft2) {
        this.ft2 = ft2;
    }

    @Override
    public String call() throws Exception {
        log.info("T1:洗水壶...");
        SleepTools.second(1);

        log.info("T1:烧开水...");
        SleepTools.second(15);

        String result = ft2.get();
        log.info("T1:拿到茶叶：{}", result);

        log.info("T1:泡茶...");
        return "上茶:" + result;
    }
}

// 洗茶壶，洗茶杯，拿茶叶
class T2Task implements Callable<String> {
    private static final Logger log = LoggerFactory.getLogger(T2Task.class);
    @Override
    public String call() throws Exception {
        log.info("T2:洗茶壶...");
        SleepTools.second(1);

        log.info("T2:洗茶杯...");
        SleepTools.second(2);

        log.info("T2:拿茶叶...");
        SleepTools.second(1);

        return "太平猴魁";
    }
}